package databasetest;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;

import javax.swing.JLabel;

public class Update extends JInternalFrame {
	Connection con;
	DbTest dt;
	Vector<String> vec=new Vector<String>();
	private JTextField serial;
	private JTextField code;
	private JTextField ea;
	private JButton btnNewButton;
	private JLabel status;
	private JButton btnNewButton_1;
	private JLabel lblCode;
	private JLabel lblEa;
	private JLabel lblSerial;

	/**
	 * Launch the application.
	 */


	/**
	 * Create the frame.
	 */
	public Update(Connection c,DbTest dt) {
		super("Update",true,true,true,false);
		setBounds(100, 100, 400, 253);
		getContentPane().setLayout(null);
		getContentPane().add(getSerial());
		getContentPane().add(getCode());
		getContentPane().add(getEa());
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getStatus());
		getContentPane().add(getBtnNewButton_1());
		getContentPane().add(getLblCode());
		getContentPane().add(getLblEa());
		getContentPane().add(getLblSerial());
		this.con=c;
		this.dt=dt;

	}


	private JTextField getSerial() {
		if (serial == null) {
			serial = new JTextField();
			serial.setBounds(143, 33, 116, 33);
			serial.setColumns(10);
		}
		return serial;
	}
	private JTextField getCode() {
		if (code == null) {
			code = new JTextField();
			code.setColumns(10);
			code.setBounds(143, 76, 116, 33);
		}
		return code;
	}
	private JTextField getEa() {
		if (ea == null) {
			ea = new JTextField();
			ea.setColumns(10);
			ea.setBounds(143, 119, 116, 33);
		}
		return ea;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("검색");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					int s=Integer.parseInt(serial.getText());
					String sql="select * from ptbl where serial =?";
					try{
						PreparedStatement pstmt=con.prepareStatement(sql);
						pstmt.setInt(1, s);
						ResultSet rs=pstmt.executeQuery();
						if(rs.next()){
							code.setText(rs.getString("code"));
							ea.setText(""+rs.getInt("ea"));
						}else{
							status.setText("데이터가 없습니다.");
						}
						
						rs.close();
						pstmt.close();
					}catch(Exception ex){
						
					}
				}
			});
			btnNewButton.setBounds(178, 176, 97, 23);
		}
		return btnNewButton;
	}
	private JLabel getStatus() {
		if (status == null) {
			status = new JLabel("New label");
			status.setBounds(12, 246, 399, 15);
		}
		return status;
	}
	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("수정");
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					int s=Integer.parseInt(serial.getText());
					String c=code.getText();
					int E=Integer.parseInt(ea.getText());
					
					String sql="update ptbl set code=?,ea=?"
							+ " where serial=?";
					try{
						PreparedStatement pstmt=con.prepareStatement(sql);
						pstmt.setString(1,c);
						pstmt.setInt(2,E);
						pstmt.setInt(3,s);
						
						pstmt.executeUpdate();
						pstmt.close();
					}catch(Exception ex){
						ex.printStackTrace();
					}
				}
			});
			btnNewButton_1.setBounds(48, 176, 97, 23);
		}
		return btnNewButton_1;
	}
	private JLabel getLblCode() {
		if (lblCode == null) {
			lblCode = new JLabel("Code");
			lblCode.setBounds(32, 39, 99, 15);
		}
		return lblCode;
	}
	private JLabel getLblEa() {
		if (lblEa == null) {
			lblEa = new JLabel("Ea");
			lblEa.setBounds(32, 82, 99, 15);
		}
		return lblEa;
	}
	private JLabel getLblSerial() {
		if (lblSerial == null) {
			lblSerial = new JLabel("Serial");
			lblSerial.setBounds(32, 125, 99, 15);
		}
		return lblSerial;
	}
	public void setData(String s,String c,String e){
		serial.setText(s);
		code.setText(c);
		ea.setText(e);
	}
//	public void setD(Vector<String> row) {
//		this.vec=row;
//		code.setText(vec.get(0));
//		ea.setText(vec.get(1));
//		serial.setText(vec.get(2));
//	}
}
